
package com.ming.MathTest;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;


public class RandomName {
    //主面板
    JFrame rFrame = new JFrame("随机点名器");
    //名字
    String[] userName = {"罗涛", "唐春", "宁晓东", "查宗威", "罗云", "何晓华", "彭婷", "代季廷", "苗锐"};

    TreeSet<Integer> userNum = new TreeSet<>();
    TreeSet<Integer> actionsNum = new TreeSet<>();
    //用于存储名字的标签
    JLabel name = new JLabel();
    //按钮
    JButton btn = new JButton("开始点名");
    //采用的是伪随机数，大家也可以不用这个，这个在网上可以找到java随机数的设置
    Random rd = new Random();

    public void init() {
        //提示标签页面
        JLabel jt = new JLabel("随机点名");
        //设置标签居中
        jt.setHorizontalAlignment(SwingConstants.CENTER);
        //设置字体大小
        jt.setFont(new java.awt.Font("随机点名器", 1, 35));
        //设置名字显示的标签居中
        name.setHorizontalAlignment(SwingConstants.CENTER);
        //通过匿名类实现Action按钮的监听事件
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                //获取随机的姓名
                String n = getRandomName();
                //设置name标签的文字
                name.setText(n);
                //设置字体
                name.setFont(new java.awt.Font(n, 1, 30));
                //设置字体颜色
                name.setForeground(Color.red);
            }

        });
        //获取JFrame的面板
        Container p = this.rFrame.getContentPane();
        //设置布局方式，我采用的BorderLayout布局
        p.setLayout(new BorderLayout(3, 1));
        //添加提示标签在北方
        p.add(jt, BorderLayout.NORTH);
        //添加姓名标签在中央
        p.add(name, BorderLayout.CENTER);
        //添加按钮控件在南方
        p.add(btn, BorderLayout.SOUTH);
        //调整大小，这个是java中无法设置标签的大小
        rFrame.pack();
        //设置窗体大小
        rFrame.setSize(1750, 600);
        //设置可以显示
        rFrame.setVisible(true);


    }

    //获取随机的姓名
    public String getRandomName() {

        HashMap<Integer, Integer> map = new HashMap<>();


        for (int i = 0; i < 9000; i++) {
            Integer key = rd.nextInt(userName.length);
            if (map.containsKey(key)) {
                Integer value = map.get(key);
                value++;
                map.put(key, value);
            } else {
                map.put(key, 0);
            }
        }
        Map<Integer, Integer> sortMap = sortMapByValue(map);
        Iterator<Map.Entry<Integer, Integer>> iterator = sortMap.entrySet().iterator();

        StringBuilder builder = new StringBuilder("");

        while (iterator.hasNext()) {
            Map.Entry<Integer, Integer> entry = iterator.next();
            builder.append(userName[entry.getKey()] + ":" + entry.getValue() + "   ").toString();
        }

        return builder.toString();


//        int i1 = 0;
//        int i2 =0;
//        int i3 =0;
//        while ( i1 == i2 || i2 == i3 || i1 == i3){
//             i1 = rd.nextInt(userName.length);
//             i2 = rd.nextInt(userName.length);
//             i3 = rd.nextInt(userName.length);
//        }


//        return userName[i1] +"  "+
//         userName[i2] + "  "+ userName[i3]
//                ;
    }

    public static void main(String[] args) {
        RandomName rn = new RandomName();
        rn.init();
    }


    /**
     * 使用 Map按value进行排序
     *
     * @param oriMap
     * @return
     */
    public static Map<Integer, Integer> sortMapByValue(Map<Integer, Integer> oriMap) {
        if (oriMap == null || oriMap.isEmpty()) {
            return null;
        }
        Map<Integer, Integer> sortedMap = new LinkedHashMap<Integer, Integer>();
        List<Map.Entry<Integer, Integer>> entryList = new ArrayList<Map.Entry<Integer, Integer>>(
                oriMap.entrySet());
        Collections.sort(entryList, new MapValueComparator());

        Iterator<Map.Entry<Integer, Integer>> iter = entryList.iterator();
        Map.Entry<Integer, Integer> tmpEntry = null;
        while (iter.hasNext()) {
            tmpEntry = iter.next();
            sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
        }
        return sortedMap;
    }

}

class MapValueComparator implements Comparator<Map.Entry<Integer, Integer>> {

    @Override
    public int compare(Map.Entry<Integer, Integer> me1, Map.Entry<Integer, Integer> me2) {

        return me2.getValue().compareTo(me1.getValue());
    }


}
